<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class SwitchSessionLang
{
    /**
     * Handle an incoming request.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Closure $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        $lang = $request->input('lang');

        if ($lang) {

            session(['lang' => in_array($lang, array_keys(system_lang())) ? $lang : null]);
        }

        return $next($request);
    }
}
